﻿#include "WebApi.h"

WebApi::WebApi(QObject *parent) : MyObject(parent)
{

}

void WebApi::WebHandler(const QPointer<JQHttpServer::Session> &session){
    QString method = session->requestMethod();
    QString url = session->requestUrlPath();
    url= url.replace("/api/", "");

    if(url == "GetSysLogList"){
        this->GetSysLogList(session);
    }
}

int WebApi::ToInt(QJsonValue value){
    if(value.isString()){
        return value.toString().toInt();
    }else{
        return value.toInt();
    }
}


/** 日志列表*/
void WebApi::GetSysLogList(const QPointer<JQHttpServer::Session> &session){
    QMap<QString, QString> param = session->requestUrlQuery();

    SysLog log;
    log.createTime = QDateTime::currentDateTime();
    if(param.contains("startDate") && !param.value("startDate").isEmpty()){
        log.createTime = QDateTime::fromString(param.value("startDate"), "yyyy-MM-dd");
    }

    QList<SysLog> list = globalData->sqlBase->GetSysLogList(log);

    QJsonArray array;
    for(SysLog info : list){
        QJsonObject json;
        json.insert("id", info.id);
        json.insert("type", info.type);
        json.insert("typeText", info.GetTypeText());
        json.insert("operater", info.operater);
        json.insert("describe", info.describe);
        json.insert("createTime", info.createTime.toString("yyyy-MM-dd HH:mm:ss"));
        array.append(json);
    }

    QJsonObject root;
    root.insert("code", "0");
    root.insert("msg", "成功");
    root.insert("data", array);
    root.insert("count", array.size());

    session->replyJsonObject(root);
}
